<!DOCTYPE HTML>
<html>
<head>
    {include file="common:meta"}
</head>
<style>
    .count-txt{
        text-align: center;
        font-size: 15px;
        font-weight: bold;
        margin-top: 1%;
    }
    #batch-deal-with{
        width: 100%;
        text-align: center;
        margin:40px auto;
        display: none;
    }
</style>
<body>
<div class="page-container">
    <div class=" float-e-margins">
        <div class="ibox-content">
            <div class="tabs-container">

                <ul class="nav nav-tabs">
                    <li class="active"><a data-toggle="tab" href="#tab-1" aria-expanded="true">指定用户</a>
                    </li>
                    <li class=""><a data-toggle="tab" href="#tab-2" aria-expanded="false">添加指定</a>
                    </li>
                </ul>
                <div class="tab-content">

                    <div class="count-txt">

                        奖池剩余球币：<span>{$prize_pool}</span>
                        <button class="update-pool btn btn-primary btn-sm" type="button" >更新奖池</button>
                        <!--<button class="edit-pool btn btn-primary btn-sm" type="button" >修改奖池</button>-->

                    </div>

                    <div class=" tab-pane active" id="tab-1">
                        <table class="table table-bordered">
                            <thead>
                            <tr>
                                <th>主键</th>
                                <th>ID</th>
                                <th>用户</th>
                                <th>宝箱ID</th>
                                <th>奖品名称</th>
                                <th>指定次数</th>
                                <th>已抽次数</th>
                                <th>状态</th>
                                <th>指定时间</th>
                                <th>抽中时间</th>
                                <th>操作人员</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {volist name='data' id='vo' key='k'}
                            <tr>
                                <td>{$vo['user_id']}</td>
                                <td>{$vo['u_id']}</td>
                                <td>{$vo['user_nickname']}</td>
                                <td>{$vo['gold_egg_id']}</td>
                                <td>{$vo['prize_name']}</td>
                                <td>{$vo['loop']}</td>
                                <td>{$vo['loop_now']}</td>
                                <td>{$vo['status']}</td>
                                <td>{$vo['create_time']}</td>
                                <td>{$vo['update_time']}</td>
                                <td>{$vo['operator']}</td>
                                <td>
                                    {if $vo['status'] == '抽奖中' }
                                    <button class="cancel btn btn-warning btn-sm btn-margin">点击取消</button>
                                    {elseif $vo['status'] == '已抽中' /}
                                    <button class=" btn btn-info btn-sm btn-margin">已抽中</button>
                                    {else /}
                                    <button class=" btn btn-info btn-sm btn-margin">已取消</button>
                                    {/if}
                                </td>

                            </tr>
                            {/volist}
                            </tbody>
                        </table>
                    </div>

                    <div class=" tab-pane" id="tab-2">
                        <div class="ibox-content">
                            <form class="form-horizontal" id="form" data-url="/AppointUserAdd">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label" >
                                        <span class="c-red">*</span>用户ID：
                                    </label>
                                    <div class="col-md-2 col-sm-5">
                                        <input type="number" class=" form-control"  name="u_id">
                                    </div>
                                </div>
                                <div class="form-group">

                                    <div class="col-md-2 col-sm-4">
                                        <input type="number" class="form-control"  name="total_num[]" placeholder="抽奖次数">
                                    </div>

                                    <div class="col-md-2 col-sm-3">
                                        <select class="gift_type form-control" name="gift_type[]" >
                                            <option value="1">礼物</option>
                                            <!--<option value="3">道具</option>-->
                                        </select>
                                    </div>

                                    <div class="col-md-2 col-sm-4">
                                        <select class="gift_id form-control" name="id[]" >
                                        </select>
                                    </div>
                                    <button type="button"  class="add btn btn-primary btn-sm" style="margin-right:15px;">+</button>
                                </div>

                                <input type="hidden" name="gold_id" value="{$gold_id}">
                                <input type="hidden" name="mode_type" value="{$mode_type}">
                                <div class="form-group">
                                    <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                                        <button class="btn-submit btn btn-primary" type="button" onclick="">添加
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="ibox-content" id="batch-deal-with">
    <form class="form-horizontal" data-url="">
        <div class="form-group">
            <label class="col-sm-2 control-label" for="">
                <span class="c-red">*</span>类型：
            </label>
            <div class="col-md-2 col-sm-10">
                <select class="form-control" name="type" >
                    <option value="1">增加</option>
                    <option value="2">减少</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="">
                <span class="c-red">*</span>数值：
            </label>
            <div class="col-md-2 col-sm-10">
                <input type="number" class="form-control"  name="prize_pool" min="0" placeholder="">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label" for="">
                <span class="c-red">*</span>原因必填：
            </label>
            <div class="col-md-2 col-sm-10">
                <textarea name="content" id="" cols="50" rows="10"></textarea>
            </div>
        </div>

        <div class="form-group">
            <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
                <button class="btn btn-success btn-sm" id="agree-btn">提交</button>
            </div>
        </div>
    </form>
</div>
{include file="common:footer"}
<script>


    $(document).ready(function () {
        getGiftInfo(1);
    });

    //切换礼物
    $('.gift_type').change(function () {
        var gift_type = $(this).val();
        getGiftInfo(gift_type);
    });
    function getGiftInfo(type)
    {
        var gold_id = $("input[name='gold_id']").val();
        var mode_type = $("input[name='mode_type']").val();

        $.ajax({
            type: 'get',
            dataType: 'json',
            url: '/GetGoldGift',
            data: {
                'gift_type': type,
                'gold_id': gold_id,
                'mode_type': mode_type
            },
            success: function (re) {
                if (re.code === 100) {

                    console.log(re);
                    var htmlText = "<option value='' selected>--请选择--</option>";
                    if(re.data !== null){

                        for(var i in re.data){

                            htmlText+="<option value="+ re.data[i]['id']+">"+ re.data[i]['name']+"---"+ re.data[i]['diamonds']+"</option>";
                        }
                        $('.gift_id:last').html(htmlText);
                    }
                }else{
                    layer.msg('错误信息',{
                        icon:2,
                        time:1500
                    });
                }
            },
            error: function (data) {
                layer.msg('服务器出小差了',{
                    icon:2,
                    time:1500
                });
            }
        });
    }


    //添加自定义内容
    $('.btn-submit').click(function(){
        console.log($('#form').serialize());
        layer.confirm('确定添加吗？',{
            title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
            btn: ['确定','取消'] //按钮
        },function(){
            $.ajax({
                type:'post',
                url:'/AppointUserAdd',
                async:true,
                data:$('#form').serialize(),
                dataType:'json',
                success:function(result){
                    if(result.code === 100){
                        layer.msg(result.msg,{
                            icon:1,
                            time:1000
                        },function () {
                            window.location.reload();
                        });
                    }
                    else{
                        layer.msg(result.msg,{
                            icon:2,
                            time:1000
                        });
                    }
                },
                error:function(err){
                    layer.msg('服务器开小差了！',{
                        time:1500
                    });
                    window.location.reload();
                }
            });
        });
    });
    //删除
    $('.cancel').click(function () {
        var gold_id = $("input[name='gold_id']").val();
        var mode_type = $("input[name='mode_type']").val();
        var user_id =  $(this).parent().siblings().eq(0).text();
        var loop =  $(this).parent().siblings().eq(5).text();

        layer.confirm('确认取消指定吗？',{
            title:['提示','font-size:18px;font-family:楷体;font-weight:700;'],
            btn: ['确定','取消'] //按钮
        },function(){
            $.ajax({
                type:'get',
                url:'/CancelAppointUser',
                async:true,
                data:{
                    gold_id:gold_id,
                    mode_type:mode_type,
                    user_id:user_id,
                    loop:loop
                },
                dataType:'json',
                success:function(result){
                    if(result.code === 100){
                        layer.msg(result.msg,{
                            icon:1,
                            time:1000
                        },function () {
                            window.location.reload();
                        });
                    }
                    else{
                        layer.msg(result.msg,{
                            icon:2,
                            time:1000
                        });
                    }
                },
                error:function(err){
                    layer.msg('服务器开小差了！',{
                        time:1500
                    });
                }
            });
        });
    });

    $('.update-pool').click(function () {
        window.location.href = 'https://api.quniuyuyin.com/V2/Public/updatePrizePool';
        window.location.reload();
    });

    $('.add').click(function(){

        var parent = $(this).parent();
        console.log(parent[0]);
        var htmlText =
            '<div class="form-group">' +
            '<div class="col-md-2 col-sm-4">'+
            '<input type="number" class="form-control" name="total_num[]" placeholder="抽奖次数">'+
            '</div>'+
            ' <div class="col-md-2 col-sm-3">'+
            '<select class="gift_type form-control" name="gift_type[]" >'+
            '<option value="1">礼物</option>'+
            '</select>'+
            '</div>' +
            '<div class="col-md-2 col-sm-4">'+
            '<select class="gift_id form-control" name="id[]" >'+
            '</select>'+
            '</div>'+
            '<button type="button"  class="reduce btn btn-primary btn-sm" style="margin-right:15px;">-</button>' +
            '</div>';
        $(this).parent().append(htmlText);
        getGiftInfo(1);
        reduce();
        changeGift();
    });
    function reduce() {
        $('.reduce').click(function () {
            $(this).parent().remove();
        })
    }

    function changeGift(){
        $('.gift_type').change(function () {
            var gift_type = $(this).val();
            getGiftInfo(gift_type);
        });
    }


    $('body').on('click','.reduce',function () {
        $(this).parent().remove();
    });

    //页面层
    $('.edit-pool').click(function () {
        layer.open({
            type: 1,
            skin: 'layui-layer-rim', //加上边框
            area: ['80%', '80%'], //宽高
            cancel:function(){
                $('#batch-deal-with').css('display','none');
            },
            content:  $('#batch-deal-with')
        });
    })


    $('#agree-btn').click(function () {

        var prize_pool = $("input[name=prize_pool]").val();
        var gold_id = $('input[name=gold_id]').val();
        var content = $('textarea[name=content]').val();
        var type = $('select[name=type]').val();

        console.log(type);

        $.ajax({
            type:'post',
            url:'/EditPrizePool',
            async:true,
            data:{
                'prize_pool':prize_pool,
                'gold_id':gold_id,
                'content':content,
                'type':type
            },
            dataType:'json',
            success:function(result){
                if(result.code == 100){
                    layer.msg('成功！',{
                        icon:1,
                        time:1000
                    });
                }else{
                    layer.msg(result.msg,{
                        icon:2,
                        time:10000
                    });
                }
                location.reload();
            },
            error:function(err){
                layer.msg('服务器开小差了！',{
                    time:1500
                });
            }
        });
    })

</script>
</body>
</html>